package generic.net;

/**
 * A Response to a Message
 *
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class Response extends Message {
    private int id;
    private String serviceProvider;
    private Status status;
    private Object payload;

    /**
     * Empty constructor should only be used by XMLDecoder.
     */
    public Response() {
        type = "RESPONSE";
    }

    /**
     * Create a response to a request with the given payload.
     * @param request   The request
     * @param payload   The payload
     */
    Response(Request request, Object payload) {
        init(request);
        this.payload = payload;
    }

    /**
     * Create an empty response to a request
     * @param request   The request
     */
    Response(Request request) {
        init(request);
    }

    /**
     * Generic init.
     */
    private void init(Request request) {
        type = "RESPONSE";
        id = request.getID();
        serviceProvider = request.getServiceProvider();
        status = Status.OK;
    }

    /**
     * Every request/response pair has a unique identifier (between each set of peers).
     * @return the unique ID for this response.
     */
    public int getID() {
        return id;
    }

    /**
     * Sets the unique identifier used by a request/response pair. As you can
     * Imagine this is a bad idea 99% of the time. Here for XMLDecoder.
     * @param id
     */
    public void setID(int id) {
        this.id = id;
    }

    /**
     * @return The name of the service provider
     */
    public String getServiceProvider() {
        return serviceProvider;
    }

    /**
     * Sets the name of the service provider.
     * @param serviceProvider the name of the service provider.
     */
    public void setServiceProvider(String serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    /**
     * gets the response payload.
     * @return  The response payload.
     */
    public Object getPayload() {
        return payload;
    }

    /**
     * sets the response payload
     * @param payload   The payload.
     */
    public void setPayload(Object payload) {
        this.payload = payload;
    }

    /**
     * Requests are not always successfull, occasionally they may cause errors.
     * Status is there to reflect this.
     * @return The status of the response.
     */
    public Status getStatus() {
        return status;
    }

    /**
     * Sets the status of this response.
     * @param status
     */
    public void setStatus(Status status) {
        this.status = status;
    }

    /**
     * A response can have any one of these statuses:
     */
    public enum Status {OK, NO_SUCH_SERVICE, ACCESS_DENIED, NO_REPLY, BAD_ARGUMENT, NO_SUCH_TYPE};
}
